//
//  EvaluateViewController.m
//  PCNews
//
//  Created by PengchengWang on 16/3/1.
//  Copyright © 2016年 pengchengWang. All rights reserved.
//

#import "EvaluateViewController.h"
#import "BaseModel.h"
#import "EvaluateTableViewCell.h"
#import "URLRequestManager.h"
#import <MJRefresh/MJRefresh.h>
#import <UIImageView+WebCache.h>
#import "DetailViewController.h"

#define kLastID 0
@interface EvaluateViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *tableV;

@property(nonatomic,strong)NSMutableArray *arrAllData;
///数据页数.表示下次请求第几页的数据.
@property (assign, nonatomic) NSInteger lastID;


@end

@implementation EvaluateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBeginViews];
    [self dataBound];
    [self setViews];
}

-(void)setBeginViews{
    self.lastID = kLastID;
}

///绑定tableView数据
-(void)dataBound{
    //创建请求第几页的数据URL
    NSString *urlStr = [NSString stringWithFormat:@"http://m.mydrivers.com/app/newslist.aspx?hd=1&minId=%ld&maxId=0&ver=2.07&temp=141083592670.623810",(long)self.lastID];
    [URLRequestManager requestUrlWithType:GET strURL:urlStr condition:nil success:^(id item) {
        //当请求到最后一条数据ID为"0"时,证明请求第一组数据,清空大数组
        if (self.lastID == kLastID) {
            self.arrAllData = nil;
        }
        for (NSDictionary *dic in item) {
            BaseModel *model = [[BaseModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrAllData addObject:model];
        }
        //取得当前获取到的数组中最后一条数据的ID,作为请求下一组数据的参数(接口文档要求)
        BaseModel *model = self.arrAllData[self.arrAllData.count-1];
        self.lastID = model.id;
        [self.tableV reloadData];
        [self endRefresh];
    } faile:^(NSError *error) {
        [self endRefresh];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"您的网络不给力!";
        [hud hide: YES afterDelay: 2];
    }];
}

-(void)endRefresh{
    [self.tableV.mj_header endRefreshing];
    [self.tableV.mj_footer endRefreshing];
}

-(void)setViews{
    self.navigationItem.title = @"评测";
#pragma mark - 设置navigationController的title颜色
    //创建颜色
    UIColor * color = [UIColor whiteColor];
    //创建NSDictionary,对应key和颜色
    NSDictionary * dict = [NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
    //设置颜色
    self.navigationController.navigationBar.titleTextAttributes = dict;
    self.tableV = [[UITableView alloc]initWithFrame:self.view.frame];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    [self.tableV registerNib:[UINib nibWithNibName:@"EvaluateTableViewCell" bundle:nil] forCellReuseIdentifier:@"EvaluateTableViewCell"];
    self.tableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.lastID = kLastID;
        [self dataBound];
    }];
    self.tableV.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self dataBound];
    }];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrAllData.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    EvaluateTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EvaluateTableViewCell" forIndexPath:indexPath];
    BaseModel *model = self.arrAllData[indexPath.row];
    cell.lblTitle.text = model.title;
    cell.lblDesc.text = model.desc;
    cell.lblReviewcount.text = [NSString stringWithFormat:@"评论[%@]",model.reviewcount];
    [cell.imgIcon sd_setImageWithURL:[NSURL URLWithString:model.icon]];
    cell.lblPostdate.text = [model.postdate substringToIndex:9];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BaseModel *model = self.arrAllData[indexPath.row];
    NSString *strDetailUrl =[NSString stringWithFormat:@"http://m.mydrivers.com/app/newsdetail.aspx?id=%ld&ver=1.1",model.id];
    DetailViewController *detailVC = [[DetailViewController alloc]initWithDetailUrl:strDetailUrl];
    [self.navigationController pushViewController:detailVC animated:YES];
}

-(NSMutableArray *)arrAllData{
    if (_arrAllData == nil) {
        _arrAllData = [[NSMutableArray alloc]init];
    }
    return _arrAllData;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
